package com.hex.ds.hdrs.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Package com.hex.ds.hdrs.common.config
 * @ClassName SchedulerConfig
 * @Description 配置Spring定时任务线程池调度
 * @Author Yin.Yang
 * @Date 2024/5/27 10:34
 * @Version v1.0
 **/
@Configuration
public class SchedulerConfig {

    @Value("${task.pool.core-pool-size}")
    private int CORE_POOL_SIZE;

    @Value("${task.pool.keep-alive-seconds}")
    private int KEEP_ALIVE_TIME;

    @Bean
    public ThreadPoolTaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(CORE_POOL_SIZE);
        scheduler.setThreadNamePrefix("scheduled-task-");
        scheduler.setAwaitTerminationSeconds(KEEP_ALIVE_TIME);
        scheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        scheduler.initialize();
        return scheduler;
    }

}
